/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Period;
import test.AlarmClock;

/**
 *
 * @author niennd
 */
abstract class Event {

    DateTime timeStart;
    Duration duration;
    Period period;
    ScheduledExecutorService fScheduler;

    public Event(DateTime timeStart, Duration duration, Period period) {
        this.timeStart = timeStart;
        this.duration = duration;
        this.period = period;
        fScheduler = Executors.newScheduledThreadPool(1);
    }

    /*
     * run event 
     */
    /*
     * start
     */
    public void start(){
        
    }
    /*
     * update
     */
    public void update() {
        
    }
    /*
     * end
     */
    public void end() {
        
    }
    
    /*
     * run schedule
     */
    void executeSchedule() {          
        start();
        // Create update schedules
        ArrayList<ScheduledFuture<?>> listUpdateSched = new ArrayList<ScheduledFuture<?>>();
        UpdateTask exUpdateTask = new UpdateTask(new Command() {
            @Override
            public void execute() {
                update();
            }
        });
        ScheduledFuture<?> updateTask1 = fScheduler.scheduleWithFixedDelay(exUpdateTask, 1, 1, TimeUnit.SECONDS );
        listUpdateSched.add(updateTask1);
        // Create stop schedule
        StopTask stopTask = new StopTask(listUpdateSched, fScheduler, new Command() {
            @Override
            public void execute() {
                end();
            }
        });
        fScheduler.schedule(stopTask, duration.getStandardSeconds(), TimeUnit.SECONDS);
    } 
}
